let fnAjaxError = function (XMLHttpRequest, textStatus, errorThrown) {
  fnStopLoading()
  console.log(XMLHttpRequest)
  console.log(textStatus)
  console.log(errorThrown)
  layer.msg('服务器内部错误')
}

let fnApiAjax = function (method, url, data, dataType, suc, err) { // ajax请求方法
  data = data || null
  dataType = dataType || 'json'
  suc = suc || null
  err = err || null
  if (url.indexOf('?') > -1) {
    url = url + '&_async=1'
  }else {
    url = url + '?_async=1'
  }
  $.ajax({
    type: method,
    url: api + url,
    data: data,
    dataType: dataType,
    crossDomain: true,
    xhrFields: {
      withCredentials: true
    },
    success: function (response, status, xhr) {
      console.log(status)
      if (status == 419) {
        layer.msg('请刷新页面重试')
        return
      }
      console.log('code:' + response.code + ' msg:' + response.msg)
      if (typeof response === 'string') {
        response = $.parseJSON(response)
      }
      if (response.code == 0) {
        if (typeof suc === 'function') {
          suc(response.data)
        }
      }
      else if (response.code == 90001) {
        layer.msg(response.msg)
        fnRedirect(response.data.redirect, 500)
      }else {
        layer.msg(response.msg)
        err(response.data)
      }
    },
    error: fnAjaxError,
    complete: fnAjaxComplete
  })
}

let fnUploadAjax = function (method, url, data, dataType, suc, err) {
  data = data || null
  dataType = dataType || 'json'
  suc = suc || null
  err = err || null
  if (url.indexOf('?') > -1) {
    url = url + '&_async=1'
  }else {
    url = url + '?_async=1'
  }

  $.ajax({
    type: method,
    url: api + url,
    data: data,
    dataType: dataType,
    crossDomain: true,
    processData: false,
    contentType: false,
    xhrFields: {
      withCredentials: true
    },
    success: function (response, status, xhr) {
      console.log(status)
      if (status == 419) {
        layer.msg('请刷新页面重试')
        return
      }
      if (typeof response === 'string') {
        response = $.parseJSON(response)
      }
      if (response.code == 0) {
        if (typeof suc === 'function') {
          suc(response.data)
        }
      }
      else if (response.code == 90001) {
        layer.msg(response.msg)
        fnRedirect(response.data.redirect, 500)
      }else {
        console.log('code:' + response.code + ' msg:' + response.msg)
        layer.msg(response.msg)
        err(response.data)
      }
    },
    error: fnAjaxError,
    complete: fnAjaxComplete
  })
}

let reqVerify = function (req, verify) { // 参数验证
  for (field in verify) {
    // console.log(field)
    if (!req.hasOwnProperty(field)) {
      let param = verify[field]
      // msg = verify.field.errMsg
      // console.log(param)
      layer.msg(param.errMsg)
      return false
    } else {
      let val = req[field]
      // console.log(val)
      let param = verify[field]
      if (param.hasOwnProperty('required') && param.required === true) {
        if (val == '') {
          // msg = param.errMsg
          layer.msg(param.errMsg)
          return false
        }
      }
      if (param.hasOwnProperty('pattern')) {
        pattern = param.pattern
        if (!pattern.test(val)) {
          // msg = param.errMsg
          layer.msg(param.errMsg)
          return false
        }
      }
    }
  }
  return true
}

;(function ($, global) { // 请求图形验证码
  let $captcha = $('.captcha-img')
  $captcha.click(function () {
    _ajax()
  })

  let _ajax = function () {api_ajax('GET', 'api/tool/captcha', {}, 'json', suc)}

  let suc = function (data) {
    $captcha.attr('src', data.captcha_src)
  }
}(jQuery, window))

// 加载中效果
let fnLoading = function () {
  layer.load(2)
}
// 取消加载效果
let fnStopLoading = function () {
  layer.closeAll('loading')
}

let fnAjaxComplete = function () {
  layer.closeAll('loading')
  fnBtnAble()
}

let fnReload = function () {
  setTimeout(function () {
    window.location.reload()
  }, 500)
}

let fnRedirect = function (url, time) {
  setTimeout(function () {
    window.location.href = url
  }, time)
}

let fnBtnDisable = function ($btn) {
  $btn.attr('disabled', 'disabled')
}
let fnBtnAble = function () {
  $('button.submit').removeAttr('disabled')
}

let fnCommonAddSuccess = function () {
  layer.msg('添加成功')
  $('.modal').modal('hide')
  fnReload()
}

let fnCommonAddError = function () {}

let fnCommonEditSuccess = function () {
  layer.msg('修改成功')
  $('.modal').modal('hide')
  fnReload()
}

let fnCommonEditError = function () {}

let fnMarkdownToHtml = function () {
  $md = $('#md')
  $html = $('#html')
  function convert () {
    if ($md.val()) {
      $html.html(marked($md.val()))
    }
    if (typeof $html[0] !== 'undefined') {
      $md.height($html[0].scrollHeight - 12)
    }
  }
  convert()
  $md.on('keyup blur', function () {
    convert()
  })
}

fnMarkdownToHtml()
